package com.leetroll.media.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author qdy
 * @Date 2024/3/25 9:52
 * 图片处理工具
 */
public class ImgDealUtil {

    public static File getWatermark(Image image){
        String mid = "C:\\Users\\86185\\Desktop\\mid.png";
        String min = "C:\\Users\\86185\\Desktop\\min.png";
        String max = "C:\\Users\\86185\\Desktop\\max.png";
        int width = image.getWidth(null);
        if(width<=600){
            return new File(min);
        }else if(width>=2500){
            return new File(max);
        }
        return new File(mid);
    }
    /**
     * @Author qdy
     * @Description 调整水印图片尺寸，返回调整之后的水印图片
     * @Date 10:30 2024/3/25
     * @Param [inputFile, targetWidth, targetHeight]
     **/
    public static File getWatermarkImg(Image image) throws IOException {
        String Watermark = "C:\\Users\\86185\\Desktop\\hhh.png";


        //1、根据要处理的图片大小动态调整
        int width = image.getWidth(null);
        int height = image.getHeight(null);

        int targetWidth=240;
        int targetHeight=103;

        //2、目前没有对应关系，按固定的处理
        if(height<=600){
            targetWidth=163;
            targetHeight=57;
        }

        // 输入图片文件 原始水印
        File inputFile = new File(Watermark);
        BufferedImage originalImage = ImageIO.read(inputFile);

        // 创建一个新的缩放后的图片
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, originalImage.getType());
        Graphics2D g2d = resizedImage.createGraphics();

        // 绘制并调整图片大小
        g2d.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
        g2d.dispose();

        String tarImgPath = "C:\\Users\\86185\\Desktop\\WatermarkImg.png";
        FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
        // 写入到输出文件
        ImageIO.write(resizedImage, "png", outImgStream);

        return(new File(tarImgPath));
    }

    /**
     * 水印处理
     * @param image     图片对象
     * @param srcWatermark 水印内容（文字水印内容/图片水印的存放路径）
     */
    public static File manageWatermark(Image image, Image srcWatermark,String tarImgPath,String outPutPath) {
        int imgWidth = image.getWidth(null);
        int imgHeight = image.getHeight(null);
        BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
        // 加水印：
        // 创建画笔
        Graphics2D graphics = bufImg.createGraphics();
        // 绘制原始图片
        graphics.drawImage(image, 0, 0, imgWidth, imgHeight, null);

//        // 添加图片水印：
//        if (StringUtils.isEmpty(watermark)) {
//            throw new RuntimeException("图片水印存放路径不能为空");
//        }
//        Image srcWatermark = readLocalPicture(watermark);
        int watermarkWidth = srcWatermark.getWidth(null);
        int watermarkHeight = srcWatermark.getHeight(null);
        // 设置 alpha 透明度：alpha 必须是范围 [0.0, 1.0] 之内（包含边界值）的一个浮点数字
        graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.9f));
        // 绘制水印图片  坐标为中间位置
        graphics.drawImage(srcWatermark, (imgWidth - watermarkWidth+10),
                (imgHeight - watermarkHeight-8), watermarkWidth, watermarkHeight, null);
        graphics.dispose();

        // 定义存储的地址
//        String tarImgPath = "C:\\Users\\86185\\Desktop\\sy4.png";
//        String tarImgPath = "C:/Users/admin/Desktop/watermark.jpg";
        // 输出图片
        try {
            String suffix=tarImgPath.split("\\.")[1];
//            outPutPath=outPutPath+"\\"+"main.png";
            FileOutputStream outImgStream = new FileOutputStream(outPutPath);
            ImageIO.write(bufImg, "png", outImgStream);
            outImgStream.flush();
            outImgStream.close();
//            return tarImgPath;
            return new File(outPutPath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
